<?php

define('TPL_LICENSE_GENERATION_FAILED',     'include/generate_trial_license/license_generation_failed.tpl');
define('TPL_RIO_LICENSE_GENERATION_SUCCESSFUL', 'include/generate_trial_license/rio_license_generation_successful.tpl');
define('TPL_COMMON_LICENSE_GENERATION_SUCCESSFUL', 'include/generate_trial_license/license_generation_successful.tpl');
     
class GenerateLicensePage
{
    private $productName;    
    /**
     * @var Page
     */
    private $page;
    
    public function __construct( $productName, $templateName )
    {
        $this->productName = $productName;
        
        $this->page = Pages::CreatePage( $templateName );
        $page = $this->page;
        $page->SetVar( "pageName", $templateName );
        $page = $this->PreparePage();
        $page->Display();
    }
    
    /**
     * @return Page
     */
    public function PreparePage()
    {       
        $form = new GenerateTrialLicenseForm();
    
        if (Request::IsMethod(Request::POST))
        {
            $this->HandlePost( $form );
        }
    
        $page = $this->page;
        $page->SetVar('FORM', $form->Generate());
        
        return $page;
    }
    
    /**
     * @param GenerateTrialLicenseForm $form
     */
    private function HandlePost( GenerateTrialLicenseForm $form )
    {        
        $form->Bind();
        if ( !$form->IsValid())
        {
            return;
        }
        
        $serial = $this->GenerateSerial( $form );
        if (!empty($serial))
        {
            $this->PrepareSuccesPage( $form, $serial ); 
        }
        else
        {
            $this->PrepareErrorPage( $form );
        }        
    }
    
    private function GenerateSerial( GenerateTrialLicenseForm $form )
    {
        $values = $form->GetValues();
        return self::GetSerial($values, $this->productName);        
    }
    
    private function PrepareSuccesPage( GenerateTrialLicenseForm $form, $serial )
    {        
        $productInfo = iSpringServices::GetProductInfo($this->productName);
        $values = $form->GetValues();

        $page = $this->page;
        $page->SetQuotedVar('PRODUCT_NAME', $productInfo->name);
        $page->SetQuotedVar('LICENSE_TYPE', "Trial");
        $page->SetQuotedVar('NAME', $values['name']);
        $page->SetQuotedVar('EMAIL', $values['email']);
        $page->SetQuotedVar('SERIAL', $serial);
        $page->SetQuotedVar('URL', $productInfo->url);
        $page->SetQuotedVar('ZIP_URL', $productInfo->zipUrl);
        
        $tplSuccess = $this->GetSuccessTemplate();        
        $page->SetBodyTemplate($tplSuccess);  
    }
    
    private function GetSuccessTemplate()
    {
        return ($this->productName == Product::RIO) ?  TPL_RIO_LICENSE_GENERATION_SUCCESSFUL : TPL_COMMON_LICENSE_GENERATION_SUCCESSFUL;
    }
    
    private function PrepareErrorPage( GenerateTrialLicenseForm $form )
    {
        $page = $this->page;
        
        $page->SetVar('ADD_BLOCK', true);
        $page->ParseTemplate('ERROR_MESSAGE', TPL_LICENSE_GENERATION_FAILED);
        
        $form->SetError(null, $page->GetTemplateEngine()->getGenerated('ERROR_MESSAGE'));        
    }
    
    /**
     * Gets generated serial number for product using current request params
     * @return String serial number
     */
    private static function GetSerial($values, $productName)
    {
        $visitorCookie = new Cookie(VISITOR_COOKIE_NAME);
        $visitorUid = $visitorCookie->GetValue(VISITOR_UID_COOKIE_INDEX);
        $licenseType = License::TRIAL;
        $productInfo = iSpringServices::GetProductInfo($productName);
        $product = $productInfo->shortName;
        $version = $productInfo->version;
        $name = $values['name'];
        $email = $values['email'];
        $phone = $values['phone'];
        $serial = iSpringServices::CreateLicense(
            Gate::FREE_TRIAL, $product, $licenseType, $version, $name, $email, $phone,
            Request::GetRemoteAddress(), $visitorUid
        );
        return $serial;
    }
}

?>